package com.wego.controller;

import com.wego.bean.StateBean;
import com.wego.bean.TableBean;
import com.wego.common.bean.PageBean;
import com.wego.common.formatter.LocalDateFormatter;
import com.wego.common.formatter.LocalDateTimeFormatter;
import com.wego.entity.domain.Carousel;
import com.wego.entity.domain.User;
import com.wego.entity.query.UserQuery;
import com.wego.entity.vo.UserVO;
import com.wego.ex.GlobalException;
import com.wego.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


/**
 * 用户表控制器
 *
 * @author Li Chuanwei
 */
@Controller
@RequestMapping("manager/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public @ResponseBody TableBean list(UserQuery userQuery) {
        //构造动态查询条件
        PageBean<User> userPageBean = userService.selectPage(userQuery);
        PageBean<UserVO> pageBean = userPageBean2UserVOPageBean(userPageBean);
        List<UserVO> userVOS = pageBean.getResult();
        Long count = pageBean.getTotal();
        TableBean tableBean = new TableBean<UserVO>();
        tableBean.setCode(0);
        tableBean.setMsg("");
        tableBean.setData(userVOS);
        tableBean.setCount(count);
        return tableBean;
    }

    private PageBean<UserVO> userPageBean2UserVOPageBean (PageBean<User> pageBean) {
        PageBean<UserVO> userVOPageBean = new PageBean<>();
        userVOPageBean.setPageNum(pageBean.getPageNum());
        userVOPageBean.setPageSize(pageBean.getPageSize());
        userVOPageBean.setPages(pageBean.getPages());
        userVOPageBean.setTotal(pageBean.getTotal());

        List<UserVO> list = new ArrayList<>();
        for (User user : pageBean.getResult()) {
            final UserVO userVO = user2UserVO(user);
            list.add(userVO);
        }
        userVOPageBean.setResult(list);

        return userVOPageBean;
    }

    private UserVO user2UserVO(User user) {
        if (user == null) {
            return null;
        }
        UserVO userVO = new UserVO();
        userVO.setId(user.getId());
        userVO.setNickname(user.getNickname());
        userVO.setAvatar(user.getAvatar());
        userVO.setGender(user.getGender());
        userVO.setBirth(LocalDateFormatter.formatLocalDate(user.getBirth(),"yyyy年MM月dd日"));
        userVO.setEmail(user.getEmail());
        userVO.setPhone(user.getPhone());
        userVO.setAccount(user.getAccount());
        userVO.setPriority(user.getPriority());
        userVO.setState(user.getState());
        userVO.setCreateTime(LocalDateTimeFormatter.formatLocalDateTime(user.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
        userVO.setUpdateTime(LocalDateTimeFormatter.formatLocalDateTime(user.getUpdateTime(),"yyyy-MM-dd HH:mm:ss"));
        return userVO;
    }

    @GetMapping("/updateState")
    public @ResponseBody StateBean updateState(Long id, Model model) {
        User user = userService.selectByPrimaryKey(id);
        Integer state = user.getState();
        if (state == null && id == null) {
            throw new GlobalException(400, "执行修改状态时提交的参数不正确", "list");
        }
        //修改状态的值
        user.setState(state == 1 ? 0 : 1);
        user.setUpdateTime(LocalDateTime.now());
        userService.updateByPrimaryKeySelective(user);
        return new StateBean(200,"更新成功");
    }
}
